<?php

namespace App\Http\Requests;
use Validator;
use Illuminate\Foundation\Http\FormRequest;
use Hash;

class AdminPost extends FormRequest
{

    /**
     *自定义验证规则 密码认证
     */
    public function addValidator()
    {
        Validator::extend('check_password', function ($attribute, $value, $parameters, $validator) {
            return Hash::check($value,\Auth::guard('admin')->user()->password);
        });
    }


    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return \Auth::guard('admin')->check();
    }

    /**
     * Get the validation rules that apply to the request.
     *  表单认证
     * @return array
     */
    public function rules()
    {
        $this->addValidator();
        return [
            'original_password'=>'required|sometimes|check_password',
            'password'=>'required|confirmed|sometimes',
            'password_confirmation'=>'required|sometimes',
        ];
    }

    /**
     * 提示信息
     */
    public function messages()
    {
        return [
            'password.required' => '密码不能为空',
            'original_password.required' => '原密码不能为空',
            'password_confirmation.required'  => '确认密码不能为空',
            'password.confirmed'  => '两次密码不一致',
            'original_password.check_password'  => '原密码不正确',
        ];
    }
}
